package com.buyouchina.service.product.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import com.buyouchina.bean.product.Product;
import com.buyouchina.service.base.DaoSupport;
import com.buyouchina.service.product.ProductService;

@Service
public class ProductServiceBean extends DaoSupport<Product> implements
		ProductService {

	String qlString;

	public List<Product> getStore(Boolean forSale, Integer outletsid,
			int firstResult, int maxResults) {
		qlString = "select p from Product p where p.forSale=?1 and p.outlets.id=?2";
		return list(qlString, firstResult, maxResults, forSale, outletsid);
	}

	public Integer getRecordCount(Boolean forSale, Integer outletsid) {
		qlString = "select count(p) from Product p where p.forSale=?1 and p.outlets.id=?2";
		return getCount(qlString, forSale, outletsid);
	}

	public List<Product> list(Boolean forSale, Integer outletsid) {
		qlString = "select p from Product p where p.forSale=?1 and p.outlets.id=?2";
		return list(qlString, forSale, outletsid);
	}

	public Boolean findByCode(String code) {
		Boolean flag = false;
		qlString = "from Product p where p.code=?1";
		if (list(qlString, code).size() > 0) {
			flag = true;
		}
		return flag;
	}

	public Product getByCode(String code, Integer oid) {
		qlString = "from Product p where p.code=?1 and p.outlets.id=?2";
		if (list(qlString, code, oid).size() > 0) {
			return list(qlString, code, oid).get(0);
		}
		return null;
	}
}
